<!-- 车型管理 -->
<template>
  <div class="dashboard-container car-models customer-list-box">
    <div class="app-container">
      <el-card shadow="never">
        <el-form
          ref="carModelsSearch"
          :model="carModelsSearchForm"
          label-width="80px"
        >
          <el-col :span="8">
            <el-form-item label="车型编号">
              <el-input
                v-model="carModelsSearchForm.id"
                placeholder="请输入车型编号"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="应载重量">
              <el-select
                v-model="carModelsSearchForm.allowableLoad"
                placeholder="请选择应载重量"
              >
                <el-option
                  v-for="item in allowableLoadOptions"
                  :key="item.value"
                  :label="item.name"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="应载体积">
              <el-select
                v-model="carModelsSearchForm.allowableVolume"
                placeholder="请选择应载体积"
              >
                <el-option
                  v-for="item in allowableVolumeOptions"
                  :key="item.value"
                  :label="item.name"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="车辆类型">
              <el-input
                v-model="carModelsSearchForm.name"
                placeholder="请输入车辆类型"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item>
              <el-button
                type="warning"
                style="margin-left: -68px;"
                @click="search"
              >搜索</el-button>
              <el-button @click="resetSearch">重置</el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </el-card>
      <el-card
        shadow="never"
        style="margin-top: 30px;"
      >
        <el-button
          type="warning"
          @click="dialogVisible = true"
        >新增车型</el-button>
        <el-table
          :data="carModelsList"
          stripe
          style="margin-top: 20px;"
        >
          <el-table-column
            type="index"
            label="序号"
            width="100"
          ></el-table-column>
          <el-table-column
            prop="id"
            label="车型编号"
            width="400"
          ></el-table-column>
          <el-table-column
            prop="name"
            label="车辆类型"
            width="150"
          ></el-table-column>
          <el-table-column
            prop="num"
            label="车型数量"
            width="150"
          ></el-table-column>
          <el-table-column
            prop="allowableLoad"
            label="应载重量（吨）"
            width="200"
          ></el-table-column>
          <el-table-column
            prop="allowableVolume"
            label="应载体积（立方米）"
            width="200"
          ></el-table-column>
          <el-table-column
            prop="measureLong"
            label="长（米）"
            width="200"
          ></el-table-column>
          <el-table-column
            prop="measureWidth"
            label="宽（米）"
            width="200"
          ></el-table-column>
          <el-table-column
            prop="measureHigh"
            label="高（米）"
            width="200"
          ></el-table-column>
          <el-table-column
            label="操作"
            align="center"
          >
            <template #default="scope">
              <el-button
                size="mini"
                type="text"
                @click="editCarModel(scope.row)"
              >编辑</el-button>
              <span>|</span>
              <el-button
                size="mini"
                type="text"
                style="color: #F56C6C;"
                @click="delCarModel(scope.row.id)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          :current-page="pageData.page"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="pageData.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="counts"
          style="margin-top: 20px;text-align: center;"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        >
        </el-pagination>
      </el-card>
    </div>
    <el-dialog
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <span slot="title">{{ addCarModelForm.id ? '编辑车型' : '新增车型' }}</span>
      <el-form
        ref="addCarModel"
        :model="addCarModelForm"
        :rules="addCarModelRules"
        label-width="80px"
        label-position="right"
      >
        <el-form-item
          label="车辆类型"
          prop="name"
        >
          <el-input
            v-model="addCarModelForm.name"
            placeholder="请输入车辆类型"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="应载重量"
          prop="allowableLoad"
        >
          <el-input
            v-model="addCarModelForm.allowableLoad"
            placeholder="请输入应载重量"
          >
            <template slot="suffix">吨</template>
          </el-input>
        </el-form-item>
        <el-form-item
          label="应载体积"
          prop="allowableVolume"
        >
          <el-input
            v-model="addCarModelForm.allowableVolume"
            placeholder="请输入应载体积"
          >
            <template slot="suffix">立方</template>
          </el-input>
        </el-form-item>
        <el-form-item label="长">
          <el-input v-model="addCarModelForm.measureLong">
            <template slot="suffix">米</template>
          </el-input>
        </el-form-item>
        <el-form-item label="宽">
          <el-input v-model="addCarModelForm.measureWidth">
            <template slot="suffix">米</template>
          </el-input>
        </el-form-item>
        <el-form-item label="高">
          <el-input v-model="addCarModelForm.measureHigh">
            <template slot="suffix">米</template>
          </el-input>
        </el-form-item>
      </el-form>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="handleClose">取 消</el-button>
        <el-button
          type="warning"
          @click="addCarModel"
        >确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { list, add, update, del } from '@/api/transit'
export default {
  data () {
    return {
      addCarModelRules: {
        name: [
          { required: true, message: '请输入车辆类型', trigger: 'blur' }
        ],
        allowableLoad: [
          { required: true, message: '请输入应载重量', trigger: 'blur' }
        ],
        allowableVolume: [
          { required: true, message: '请输入应载体积', trigger: 'blur' }
        ]
      },
      addCarModelForm: {
        allowableLoad: null,
        allowableVolume: null,
        measureHigh: 0.0,
        measureLong: 0.0,
        measureWidth: 0.0,
        name: ''
      },
      dialogVisible: false,
      carModelsList: [],
      pageData: {
        page: 1,
        pageSize: 10
      },
      counts: 0,
      carModelsSearchForm: {
        id: '',
        name: '',
        allowableLoad: '',
        allowableVolume: ''
      },
      allowableLoadOptions: [
        {
          value: 'RANGE_LEVEL_1',
          name: '<1.8（吨）'
        },
        {
          value: 'RANGE_LEVEL_2',
          name: '1.8-6（吨）'
        },
        {
          value: 'RANGE_LEVEL_3',
          name: '6-14（吨）'
        },
        {
          value: 'RANGE_LEVEL_4',
          name: '14-30（吨）'
        },
        {
          value: 'RANGE_LEVEL_5',
          name: '30-50（吨）'
        },
        {
          value: 'RANGE_LEVEL_6',
          name: '50-100（吨）'
        },
        {
          value: 'RANGE_LEVEL_7',
          name: '100>（吨）'
        }
      ],
      allowableVolumeOptions: [
        {
          value: 'RANGE_LEVEL_1',
          name: '<3（m³）'
        },
        {
          value: 'RANGE_LEVEL_2',
          name: '3-5（m³）'
        },
        {
          value: 'RANGE_LEVEL_3',
          name: '5-10（m³）'
        },
        {
          value: 'RANGE_LEVEL_4',
          name: '10-15（m³）'
        },
        {
          value: 'RANGE_LEVEL_5',
          name: '15-30（m³）'
        },
        {
          value: 'RANGE_LEVEL_6',
          name: '30-50（m³）'
        },
        {
          value: 'RANGE_LEVEL_7',
          name: '50-80（m³）'
        },
        {
          value: 'RANGE_LEVEL_8',
          name: '80-150（m³）'
        },
        {
          value: 'RANGE_LEVEL_9',
          name: '150>（m³）'
        }
      ]
    }
  },
  mounted () {
    this.getCarModelsList()
  },
  methods: {
    async getCarModelsList() {
      const res = await list(this.pageData)
      this.carModelsList = res.data.items
      this.counts = +res.data.counts
    },
    handleSizeChange(val) {
      this.pageData.pageSize = val
      this.getCarModelsList()
    },
    handleCurrentChange(val) {
      this.pageData.page = val
      this.getCarModelsList()
    },
    // 重置搜索
    resetSearch() {
      this.carModelsSearchForm = {
        id: '',
        name: '',
        allowableLoad: '',
        allowableVolume: ''
      }
      this.pageData = {
        page: 1,
        pageSize: 10
      }
      this.getCarModelsList()
    },
    // 搜索
    search() {
      this.pageData = {
        ...this.pageData,
        ...this.carModelsSearchForm
      }
      this.getCarModelsList()
    },
    // 添加车辆类型
    addCarModel() {
      this.$refs.addCarModel.validate(async valid => {
        if (valid) {
          // todo
          if (this.addCarModelForm.id) {
            await update(this.addCarModelForm.id, this.addCarModelForm)
          } else {
            await add(this.addCarModelForm)
          }
          this.getCarModelsList()
          this.$message.success(this.addCarModelForm.id ? '成功修改车型' : '成功新增车型')
          this.addCarModelForm = {
            allowableLoad: null,
            allowableVolume: null,
            measureHigh: 0.0,
            measureLong: 0.0,
            measureWidth: 0.0,
            name: ''
          }
          this.dialogVisible = false
        }
      })
    },
    handleClose() {
      this.$confirm('确认关闭？')
        .then(_ => {
          this.dialogVisible = false
          this.$refs.addCarModel.resetFields()
          this.addCarModelForm = {
            allowableLoad: null,
            allowableVolume: null,
            measureHigh: 0.0,
            measureLong: 0.0,
            measureWidth: 0.0,
            name: ''
          }
        })
        .catch(_ => {})
    },
    // 编辑车型
    editCarModel(obj) {
      this.addCarModelForm = obj
      this.dialogVisible = true
    },
    // 删除车型
    delCarModel(id) {
      this.$confirm('确定要删除吗？', '删除确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await del(id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.getCarModelsList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.add-btn{
  margin-bottom: 20px;
}
.alert {
  margin: 10px 0px;
}
.pagination {
  margin-top: 40px;
  padding-bottom: 0px;
}

.el-select {
  width: 100%;
}

.car-models ::v-deep .el-table td,
.el-table th {
  padding: 12px 0;
  min-width: 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  text-overflow: ellipsis;
  vertical-align: middle;
  position: relative;
  text-align: left;
  overflow: hidden;
}
.car-models {
  ::v-deep .el-dialog__title {
    width: 73px;
    height: 25px;
    font-size: 18px;
    font-family: PingFangSC, PingFangSC-Regular;
    font-weight: 400;
    text-align: left;
    color: #20232a;
    line-height: 25px;
    letter-spacing: 0px;
  }
  ::v-deep .el-dialog__body {
    text-align: center;
    padding: 40px 60px 0 30px;
  }
  .warn-img {
    width: 40px;
  }
  p {
    height: 24px;
    font-size: 16px;
    font-family: PingFangSC, PingFangSC-Regular;
    font-weight: 400;
    color: #818693;
    line-height: 24px;
  }
}
</style>
